package aeolus.wind.spider.web.commons.spider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class SomeBody {

    private String                    identityName;
    private Map<String, Double>       abilityAndValue;
    private Map<String, Double>       abilityAndValueInOther;
    private Map<String, List<Double>> abilityAndValueList;

    public SomeBody(String identityName) {
        this.identityName = identityName;
        this.abilityAndValue = new HashMap<String, Double>();
        this.abilityAndValueInOther = null;
        this.abilityAndValueList = new HashMap<String, List<Double>>();
    }

    public String getIdentityName() {
        return identityName;
    }

    public void addAbilityByMe(String ability, Double value) {
        abilityAndValue.put(ability, value);
    }

    /**
     * 如果是现有能力的最大值，则增加，否则不增加
     * 
     * @param ability
     * @param value
     * @return
     */
    public boolean addAbilityByMeWithMax(String ability, Double value) {
        Double myValue = abilityAndValue.get(ability);
        if (myValue == null || myValue.intValue() < value.intValue()) {
            abilityAndValue.put(ability, value);
            return true;
        }
        return false;
    }

    public void addAbilityByOther(String ability, Double value) {
        List<Double> list = abilityAndValueList.get(ability);
        if (list == null) {
            list = new ArrayList<Double>();
            abilityAndValueList.put(ability, list);
        }
        if (value != null && value > 0) list.add(value);// 为0的投票认为是废票
    }

    /**
     * 返回自己眼中的自己。结果是一个map，key是能力，value是能力得分
     * 
     * @return
     */
    public Map<String, Double> getMe() {
        return abilityAndValue;
    }

    /**
     * 返回别人眼中的自己。结果是一个map，key是能力，value是能力得分
     * 
     * @return
     */
    public Map<String, Double> getMeInOther() {
        if (abilityAndValueInOther == null) {
            abilityAndValueInOther = new HashMap<String, Double>();
            for (Entry<String, List<Double>> entryList : abilityAndValueList.entrySet()) {
                double value = 0;
                List<Double> valueList = entryList.getValue();
                if (valueList != null && valueList.size() > 0) {
                    for (Double singleValue : valueList) {
                        value += singleValue;
                    }
                    value = value / valueList.size();
                }
                abilityAndValueInOther.put(entryList.getKey(), value);
            }
        }
        return abilityAndValueInOther;
    }

}
